//
//  collectViewController.m
//  GameBoss
//
//  Created by dllo on 15/5/4.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "collectViewController.h"
#import "DataViewController.h"
#import "FocusModel.h"

@interface collectViewController ()
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *resultArr;
@property (nonatomic, copy) NSString *dataPath;
@end

@implementation collectViewController
- (void)dealloc
{
    [_tableView release];
    [_resultArr release];
    [_dataPath release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height - 113) style:(UITableViewStylePlain)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    self.dataPath = [sandBoxPath stringByAppendingPathComponent:@"collect.plist"];
    //反归档
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:self.dataPath];
    self.resultArr = [NSMutableArray arrayWithArray:arr];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:reuse] autorelease];
    }
    cell.textLabel.text = [self.resultArr [indexPath.row]title];

    return cell;
}
#pragma mark 删除_2 初始化
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
#pragma mark 删除_3 控制哪行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#pragma mark 删除_4 改名字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
#pragma mark 删除_5 删除, 对cell删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除数据源
    [self.resultArr removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    //重新归档
    [NSKeyedArchiver archiveRootObject:self.resultArr toFile:self.dataPath];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DataViewController *dataVC = [[DataViewController alloc] init];
    dataVC.name = [self.resultArr[indexPath.row] title];
    if ([self.resultArr[indexPath.row] isKindOfClass:[FocusModel class]]) {
        dataVC.focus = self.resultArr[indexPath.row];
    }
    dataVC.art = self.resultArr[indexPath.row];
    dataVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:dataVC animated:YES];
    [dataVC release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
